package com.lxm.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.lxm.entity.wx.WxAcceptEntity;

/**
 * @author ：Administrator
 * @date :创建时间 2021/12/23 20:16
 */
public class XmlUtils {
    private static final ObjectMapper xmlMapper = new XmlMapper();

    static {
        // 时间格式化
        xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        ////字段为null，自动忽略，不再序列化
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        ////XML标签名:使用骆驼命名的属性名，
        xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
    }


    static public <T> T stringXmlToBean(String xml, Class<T> valueType) throws JsonProcessingException {
        return xmlMapper.readValue(xml, valueType);
    }

    static public String beanToXml(Object o) throws JsonProcessingException {
        return xmlMapper.writeValueAsString(o);
    }

}
